package mju.cxf.config.scheduleds;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * 签到定时任务
 */
@Slf4j
@Component
public class SignScheduled {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    private static final String USER_PREFIX="user:";

    /**
     * 每天更新签到状态
     */
    @Scheduled(cron = " 0 0 0 * * *") //每天零点
    public void signScheduled(){
        log.info("每天清空签到状态");
        Set<String> signKeys = redisTemplate.keys( USER_PREFIX+ "*");
        if (signKeys != null) {
            for (String signKey : signKeys) {
                if(signKey.startsWith(USER_PREFIX)){
                    redisTemplate.opsForHash().put(signKey,"status","0");
                }
            }
        }
    }

    /**
     * 每个月清空签到情况
     */
    @Scheduled(cron = "0 0 0 1 * ?") //每个月一号零点
    public void signScheduled1(){
        log.info("每个月清空签到记录");
        Set<String> signKeys = redisTemplate.keys( USER_PREFIX+ "*");
        if (signKeys != null) {
            for (String signKey : signKeys) {
                if(signKey.startsWith(USER_PREFIX)){
                    redisTemplate.delete(signKey);
                }
            }
        }
    }
}
